# Contributor: Will Sinatra <wpsinatra@gmail.com>
# Maintainer: Will Sinatra <wpsinatra@gmail.com>
pkgname=grlx
pkgver=1.0.5
pkgrel=0
pkgdesc="effective fleet configuration management"
pkgusers="farmer"
pkggroups="farmer"
url="https://github.com/gogrlx/grlx"
arch="all"
license="0BSD"
makedepends="go"
subpackages="$pkgname-sprout:_sprout
	$pkgname-farmer:_farmer
	$pkgname-farmer-openrc:_sprout_rc
	$pkgname-sprout-openrc:_farmer_rc
"
source="$pkgname-$pkgver.tar.gz::https://github.com/gogrlx/grlx/archive/refs/tags/v$pkgver.tar.gz
		grlx-farmer.conf
		grlx-sprout.conf
		grlx-farmer.initd
		grlx-sprout.initd
		grlx-farmer.pre-install
		grlx-farmer.post-install
		grlx-sprout.post-install
"
options="net !check !strip" #tests require docker

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

case "$CARCH" in
	x86)     _arch=386 ;;
	x86_64)  _arch=amd64 ;;
	arm*)    _arch=arm ;;
	aarch64) _arch=arm64 ;;
	riscv64) _arch=riscv64 ;;
	ppc64le) _arch=ppc64le ;;
	s390x)   _arch=s390x ;;
esac

build() {
	GOOS=linux GOARCH=$_arch make grlx
	GOOS=linux GOARCH=$_arch make farmer
	GOOS=linux GOARCH=$_arch make sprout
}

check() {
	make test
}

_farmer() {
	pkgdesc="grlx control plane"

	mkdir -p "$subpkgdir"/var/cache/grlx/farmer
	mkdir -p "$subpkgdir"/etc/grlx/pki/farmer

	install -Dm755 "$builddir"/bin/farmer "$subpkgdir/usr/bin/grlx-farmer"
	install -Dm644 "$srcdir"/grlx-farmer.conf "$subpkgdir/etc/grlx/farmer"

	chown -R farmer:farmer "$subpkgdir"/etc/grlx
	chown -R farmer:farmer "$subpkgdir"/var/cache/grlx
}

_farmer_rc() {
	default_openrc
	pkgdesc="grlx control plane (OpenRC init scripts)"

	install -Dm 755 "$srcdir"/grlx-farmer.initd "$subpkgdir"/etc/init.d/grlx-farmer
}


_sprout() {
	pkgdesc="grlx remote control agent"

	install -Dm755 "$builddir"/bin/sprout "$subpkgdir/usr/bin/grlx-sprout"
	install -Dm644 "$srcdir"/grlx-sprout.conf "$pkgdir/etc/grlx/sprout"
}

_sprout_rc() {
	default_openrc
	pkgdesc="grlx remote control agent (OpenRC init scripts)"

	install -Dm 755 "$srcdir"/grlx-sprout.initd "$subpkgdir"/etc/init.d/grlx-sprout
}


package() {
	install -Dm755 "$builddir"/bin/grlx "$pkgdir/usr/bin/grlx"
}

sha512sums="
658eb6e90748d3673ae90f1bce458eb6c00123644c11314620cd232ca2d41f96314b0ca71838e56881e98abd2b8b39080f314083121ae1d9ea68ab738838b7b5  grlx-1.0.5.tar.gz
be3e8e8939caae4e1bf3ac2b9151485eec8761b8e2d9a44a1fae54a8c02468ac1621f8c3350a082393cf674b7b5a35379d0810108ff154dc98f9e6ea726b1205  grlx-farmer.conf
abfcba042b79d2097344c35a8f4ed150697e55ad455f025ce49ae9c666fe0b3de574d44f493a12c3bf687a926603757ba550a7eb5ef978ba293ab3b1e30fa533  grlx-sprout.conf
ac973a3a4f69be78a612eb4fa03583f2c3a1000265f29236551565013fafd2e3694d2ec7fb5cfac57c8dc12eba0bc76cd4627c2a5221907d99173a0d456dbdd1  grlx-farmer.initd
4bda260c745f46260bd26153ec13406a7aee0db7f1ff21958f38a39766df944b5f82c1cba139f9f1cd3e25f7476f7d8c87351ae697803b62ee3ecc5a9cffc172  grlx-sprout.initd
4e31d496cc3f7be6e7145478d0abaccfde7f5fc10968deb323e9e083abc051faf964abb1eadd8291169005cc288ca4915cd4db4fadd05ed2ad73f7200a0b1d43  grlx-farmer.pre-install
dd57f2c541e15e53cfaaacbb08a2d02c00854ee5d35db468a9cca6e4a9ca0258d8b4740d2040b2269e6ddc00bc55d647062dc46849bfd2983e4a7a3618527148  grlx-farmer.post-install
70719744ea18e1d8578566834177871709f3c3c88b5261641c7b6957875506764590dd0754b5f697b76bb19a3b480f0f38e2ec4821f629230fc8404ec8777d4c  grlx-sprout.post-install
"
